//
// Created by jyh on 2021/8/3.
//
#include "Com.h"
#include "Patterns.h"

struct Patterns PATTERNS;

// PATTERNS FILE
void Patterns_readFile(){
    char *line;
    FILE* in;
    line=calloc(LEN_STRING+1,sizeof(char));
    memset(PATTERNS.fform,0,sizeof(PATTERNS.fform));
    memset(PATTERNS.fname,0,sizeof(PATTERNS.fname));
    PATTERNS.MAXFILE = 100;

    /// gnss.cfg part
    if ((in=fopen(DLY.mTable.pndfln, "r"))==NULL){
        printf("file = %s,can't accsss pndfln file", DLY.mTable.pndfln);
        exit(1);
    }
    PATTERNS.nfile = 0;
    while (fgets(line, LEN_STRING, in)){
        sscanf(line,"%s%s",PATTERNS.fname[PATTERNS.nfile],PATTERNS.fform[PATTERNS.nfile]);
        PATTERNS.nfile++;
        if(PATTERNS.nfile > PATTERNS.MAXFILE){
            printf("nfile = %d,too many files defined in panda_file_name", PATTERNS.nfile);
            exit(1);
        }
    }
    free(line);
    fclose(in);
}
void Patterns_m_getPatternName(int ldefined, const char* keyword, const char* param_list, int iyear, int imonth, int iday, int ihour, char* name) {
    char varword[1024];
    char form[1024] = { '\0' };
    int npar, nword, mjd, week, wd, idoy;
    bool lfound;
    char word[40][256], param[40][256];
    int i, j, k, index;
    if (ldefined)
        strcpy(form, name);
    else {
        for (i = 0; i < PATTERNS.nfile; i++) {
            if (!strcmp(PATTERNS.fname[i], keyword)) {
                strcpy(form, PATTERNS.fform[i]);
                break;
            }
        }
        if (len_trim(form) == 0) {
            printf("keyword = %s,keyword not defined in panda_file_name",keyword);
            exit(1);
        }
    }
    split_string(false, form, ' ', ' ', '-', &nword, (char*)word, 256);
    strcpy(varword, param_list);
    split_string(true, varword, ' ', ' ', ':', &npar, (char*)param, 256);
    mjd = md_julday(iyear, imonth, iday);
    gpsweek(iyear, imonth, iday, &week, &wd);
    mjd2doy(mjd, &iyear, &idoy);
    sprintf(param[npar], "Y=%1.1d", iyear % 10);
    sprintf(param[npar + 1], "YY=%2.2d", iyear % 100);
    sprintf(param[npar + 2], "YYYY=%4.4d", iyear);
    sprintf(param[npar + 3], "DDD=%3.3d", idoy);
    sprintf(param[npar + 4], "HH=%2.2d", ihour);
    sprintf(param[npar + 5], "WWWW=%4.4d", week);
    sprintf(param[npar + 6], "GPSW=%4.4d", week);
    sprintf(param[npar + 7], "WWWWD=%5.5d", week * 10 + wd);
    sprintf(param[npar + 8], "MJD=%5.5d", mjd);
    sprintf(param[npar + 9], "MM=%2.2d", imonth);
    npar += 10;
    memset(name,0,strlen(name) * sizeof(char));
    for (i = 0; i < nword; i++) {
        if (len_trim(word[i]) == 0)
            continue;
        trim(word[i]);
        if (i % 2 != 0) {
            lfound = false;
            for (j = 0; j < npar; j++) {
                //if (strstr(param[j], word[i]) != NULL) {
                if (!strncmp(param[j], word[i],strlen(word[i]))) {
                    lfound = true;
                    k = len_trim(name);
                    index = index_string(param[j], '=');
                    substringEx(varword, param[j], index + 1,
                                strlen(param[j]) - index - 1);
                    strcpy(name + k, varword);
                    break;
                }
            }
            if (!lfound) {
                printf("word = %s,keyword = %s,word is not defined in keyword",word[i],keyword);
                exit(1);
            }
        } else {
            k = len_trim(name);
            strcpy(name + k, word[i]);
        }
    }
}